'use strict'

// import '../renderer/store'
import { app, BrowserWindow, Menu, ipcMain, Tray } from 'electron'
import Positioner from 'electron-positioner'
import path from 'path'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
let positioner
let willQuitApp = false
let tray = null
let trayIconPath = path.join(__dirname, '../../build/icons/tray.png')
let trayGreyIconPath = path.join(__dirname, '../../build/icons/tray-grey.png')

const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9081`
  : `file://${__dirname}/index.html`

const menuTemplate = [
  {
    role: 'window',
    submenu: [
      {
        label: '切换登陆',
        click () {
          mainWindow.webContents.send('logout')
        }
      },
      { role: 'quit', label: '退出' }
    ]
  }
]

if (process.platform !== 'darwin') {
  delete menuTemplate[0].role
  menuTemplate[0].label = '设置'
} else {
  menuTemplate.push({
    label: '编辑',
    submenu: [
      { role: 'cut', label: '剪切' },
      { role: 'copy', label: '复制' },
      { role: 'paste', label: '粘贴' }
    ]
  })
}

function createWindow () {
  const menu = Menu.buildFromTemplate(menuTemplate)
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 400,
    minHeight: 400,
    maxHeight: 800,
    width: 500,
    minWidth: 500,
    maxWidth: 800,
    useContentSize: true,
    titleBarStyle: 'hidden',
    webPreferences: { webSecurity: false }
  })
  positioner = new Positioner(mainWindow)

  Menu.setApplicationMenu(menu)

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  mainWindow.on('close', (e) => {
    if (!willQuitApp) {
      e.preventDefault()
      mainWindow.hide()
    }
  })

  tray = new Tray(trayIconPath)
  tray.setToolTip('Gitee Helper')
  tray.on('click', () => {
    if (mainWindow) {
      mainWindow.isVisible()
        ? mainWindow.isFocused()
          ? mainWindow.hide()
          : mainWindow.show()
        : mainWindow.show()
    }
  })
}

ipcMain.on('open-window', () => {
  mainWindow.show()
})

ipcMain.on('set-size', (e, { width = 500, height = 400 } = {}) => {
  mainWindow.setSize(width, height)
})

ipcMain.on('set-position', (e, place = 'center') => {
  positioner.move(place)
})

ipcMain.on('quit', () => {
  mainWindow.destroy()
})

ipcMain.on('set-tray-icon', (e, status) => {
  if (status === 'notify') {
    tray.setImage(trayIconPath)
  } else if (status === 'no-notify') {
    tray.setImage(trayGreyIconPath)
  }
})

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  mainWindow && mainWindow.show()
  if (mainWindow === null) {
    createWindow()
  }
})

app.on('before-quit', (e) => {
  willQuitApp = true
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
